﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Zero.UControl.Base.Enums;

namespace Zero.UControl.Component
{
    /// <summary>
    /// UDrawer.xaml 的交互逻辑
    /// </summary>
    public partial class UDrawer : UserControl
    {
        private Window _parentWindow;
        private double oldWinWidth;
        private double oldWinHeight;

        public UDrawer()
        {
            InitializeComponent();
            Loaded += UDrawer_Loaded;
            Unloaded += UDrawer_Unloaded;
        }


        #region 依赖属性
        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(UDrawer), new PropertyMetadata("标题"));


        public object Content
        {
            get { return (object)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(object), typeof(UDrawer), new PropertyMetadata(null));


        public double ContentWidth
        {
            get { return (double)GetValue(ContentWidthProperty); }
            set { SetValue(ContentWidthProperty, value); }
        }

        public static readonly DependencyProperty ContentWidthProperty =
            DependencyProperty.Register("ContentWidth", typeof(double), typeof(UDrawer), new PropertyMetadata(480d, OnSizeChanged));


        public double ContentHeight
        {
            get { return (double)GetValue(ContentHeightProperty); }
            set { SetValue(ContentHeightProperty, value); }
        }

        public static readonly DependencyProperty ContentHeightProperty =
            DependencyProperty.Register("ContentHeight", typeof(double), typeof(UDrawer), new PropertyMetadata(480d, OnSizeChanged));


        public PlacementType Placement
        {
            get { return (PlacementType)GetValue(PlacementProperty); }
            set { SetValue(PlacementProperty, value); }
        }

        public static readonly DependencyProperty PlacementProperty =
            DependencyProperty.Register("Placement", typeof(PlacementType), typeof(UDrawer), new PropertyMetadata(PlacementType.Left, OnPlacementTypeChanged));


        public bool Show
        {
            get { return (bool)GetValue(ShowProperty); }
            set { SetValue(ShowProperty, value); }
        }

        public static readonly DependencyProperty ShowProperty =
            DependencyProperty.Register("Show", typeof(bool), typeof(UDrawer), new PropertyMetadata(false, OnVisiableChanged));
        #endregion

        #region 依赖方法

        private static void OnSizeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as UDrawer)?.UpdateLayout();
        }

        private static void OnPlacementTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as UDrawer)?.UpdateLayout();
        }

        private void UpdateLayout()
        {
            if (ActualWidth == 0 || ActualHeight == 0)
            {
                return;
            }

            switch (Placement)
            {
                case PlacementType.Top:
                    bd.Width = this.ActualWidth;
                    bd.Height = ContentHeight;
                    t1.Y = -ContentHeight;
                    break;
                case PlacementType.Bottom:
                    bd.Width = this.ActualWidth;
                    bd.Height = ContentHeight;
                    t1.Y = ActualHeight;
                    break;
                case PlacementType.Left:
                    bd.Width = ContentWidth;
                    bd.Height = this.ActualHeight;
                    t1.X = -ContentWidth;
                    break;
                case PlacementType.Right:
                    bd.Width = ContentWidth;
                    bd.Height = this.ActualHeight;
                    t1.X = ActualWidth;
                    break;
                default:
                    break;
            }
        }

        private static void OnVisiableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool flag = (bool)e.NewValue;
            (d as UDrawer).Visibility = flag ? Visibility.Visible : Visibility.Hidden;
        }
        #endregion

        #region 本地事件
        private void UDrawer_Loaded(object sender, RoutedEventArgs e)
        {
            _parentWindow = Window.GetWindow(this);
            oldWinWidth = _parentWindow.ActualWidth;
            oldWinHeight = _parentWindow.ActualHeight;
            if (_parentWindow != null)
            {
                _parentWindow.SizeChanged += OnWindowSizeChanged;
            }
            UpdateLayout();
        }
     
        private void UDrawer_Unloaded(object sender, RoutedEventArgs e)
        {
            if (_parentWindow != null)
            {
                _parentWindow.SizeChanged -= OnWindowSizeChanged;
            }
        }

        private void OnWindowSizeChanged(object sender, SizeChangedEventArgs e)
        {
            double newWinWidth = _parentWindow.ActualWidth;
            double newWinHeight = _parentWindow.ActualHeight;
            double deltaWidth = newWinWidth - oldWinWidth;
            double deltaHeight = newWinHeight - oldWinHeight;

            bd.Width += deltaWidth;
            bd.Height += deltaHeight;
            UpdateLayout();
        }


        private void Close_Button_Click(object sender, RoutedEventArgs e)
        {
            SetCurrentValue(ShowProperty,false);
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SetCurrentValue(ShowProperty, false);
        }

        private void UserControl_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (Visibility == Visibility.Visible)
            {
                DoubleAnimation Animation = new DoubleAnimation();
                Animation.Duration = new Duration(TimeSpan.FromMilliseconds(300));
                switch (Placement)
                {
                    case PlacementType.Top:
                        Animation.From = -ContentHeight;
                        Animation.To = 0;
                        Animation.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
                        t1.BeginAnimation(TranslateTransform.YProperty, Animation);
                        break;
                    case PlacementType.Bottom:
                        Animation.From = ActualHeight;
                        Animation.To = ActualHeight - ContentHeight;
                        Animation.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
                        t1.BeginAnimation(TranslateTransform.YProperty, Animation);
                        break;
                    case PlacementType.Left:
                        Animation.From = -ContentWidth;
                        Animation.To = 0;
                        Animation.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
                        t1.BeginAnimation(TranslateTransform.XProperty, Animation);
                        break;
                    case PlacementType.Right:
                        Animation.From = ActualWidth;
                        Animation.To = ActualWidth - ContentWidth;
                        Animation.EasingFunction = new CubicEase() { EasingMode = EasingMode.EaseOut };
                        t1.BeginAnimation(TranslateTransform.XProperty, Animation);
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion
    }
}
